// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports


namespace SCIEF.SysBFW.CommonExt
{
	namespace Base
	{
		/// <summary>
		/// This a class to manipulate Application with the help of Metaqueries and before loggin an predined connection string in appconfig File.
		/// </summary>
		/// <remarks>
		/// <b>Roles and  Responsability </b>:This class has ....</remarks>
		public sealed class AppOption
		{
			
			private DataRow dtrow;
			
			private static MetaQuery aAppsOption;
			private static MetaQuery aLocalCulture;
			
			private Guid _languageId;
			private string _languageName;
			private string _culture;
			private string _applicationVersion;
			private string _applicationTitle;
			private static string _decimalSeparator = "";
			private static string _thousandsSeparator = "";
			
			private int _descriptionColumnIndex = 0;
			/// <summary>
			/// Depending if the user is Logged in We go get is Option. If Not We get the default application option.
			/// </summary>
			/// <remarks>If The User is not Logged in. We use the Connection String from the App.config/Web.Config</remarks>
			public AppOption()
			{
				if (!AppManager.IsConnected)
				{
					aAppsOption = MetaQuery.GetMetaQueryByName("ApplicationOption");
				}
				else
				{
					aAppsOption = MetaQuery.GetMetaQueryByName("LocalOption");
					aAppsOption.SQLFrom += string.Format(" and MetaUserOptions.UserId_FK = \'{0}\'", SCIEF.SysBFW.CommonExt.Base.AppManager.UserID);
					
				}
				
				aAppsOption.FillResultQuery();
				aAppsOption.ResultQuery.PrimaryKey = new DataColumn[] {aAppsOption.ResultQuery.Columns[aAppsOption.GUIDField]};
				
			}
			/// <summary>
			/// This is the Application title of SystemBOX , it will appear on the Splash Screen and on the TEXT of SystemBOX MDI
			/// </summary>
			/// <value></value>
			/// <returns></returns>
			/// <remarks>this propertie is returned by metaQuery.
			/// the get will assign value if not null or empty
			/// </remarks>
			public string ApplicationTitle
			{
				get
				{
					if (string.IsNullOrEmpty(_applicationTitle))
					{
						dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_APPLICATIONTITLE);
						if (dtrow != null)
						{
							_applicationTitle = dtrow["OptionValue"].ToString();
						}
					}
					return _applicationTitle;
				}
				
			}
			public string ApplicationVersion
			{
				get
				{
					if (string.IsNullOrEmpty(_applicationVersion))
					{
						dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_APPLICATIONVERSION);
						if (dtrow != null)
						{
							_applicationVersion = dtrow["OptionValue"].ToString();
						}
					}
					return _applicationVersion;
				}
				set
				{
					_applicationVersion = value;
				}
			}
			
			public static void GetSeparator()
			{
				
				DataRow dtrow = default(DataRow);
				dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_DECIMALSEPARATOR);
				if (dtrow != null)
				{
					DecimalSeparator = dtrow["OptionValue"].ToString();
				}
				dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_THOUSANDSEPARATOR);
				if (dtrow != null)
				{
					ThousandsSeparator = dtrow["OptionValue"].ToString();
				}
			}
			
			public static string DecimalSeparator
			{
				get
				{
					return _decimalSeparator;
				}
				set
				{
					_decimalSeparator = value;
					
					System.Globalization.CultureInfo Cultureclone = (System.Globalization.CultureInfo) (System.Threading.Thread.CurrentThread.CurrentCulture.Clone());
					Cultureclone.NumberFormat.NumberDecimalSeparator = value;
					System.Threading.Thread.CurrentThread.CurrentCulture = Cultureclone;
					
				}
			}
			
			public static string ThousandsSeparator
			{
				get
				{
					return _thousandsSeparator;
				}
				set
				{
					_thousandsSeparator = value;
					
					System.Globalization.CultureInfo Cultureclone = (System.Globalization.CultureInfo) (System.Threading.Thread.CurrentThread.CurrentCulture.Clone());
					Cultureclone.NumberFormat.NumberGroupSeparator = value;
					System.Threading.Thread.CurrentThread.CurrentCulture = Cultureclone;
					
				}
			}
			public string Culture
			{
				get
				{
					Guid aLanguageID = LanguageId;
					DataRow aRow = default(DataRow);
					try
					{
						_culture = "en";
						if (!aLanguageID.Equals(System.Guid.Empty))
						{
							aLocalCulture = CommonExt.MetaQuery.GetMetaQueryByName("metaStringCulture");
							aLocalCulture.SQLWhere = string.Format(" Stringid = \'{0}\' ", aLanguageID);
							aLocalCulture.FillResultQuery();
							aLocalCulture.ResultQuery.PrimaryKey = new DataColumn[] {aLocalCulture.ResultQuery.Columns[aLocalCulture.GUIDField]};
							aRow = aLocalCulture.ResultQuery.Rows.Find(aLanguageID);
							if (aRow != null)
							{
								_culture = (string) (aRow["DefaultText"].ToString());
							}
						}
						return _culture;
					}
					catch (Exception ex)
					{
						throw (ex);
					}
				}
				
				set
				{
					_culture = value;
				}
			}
			
			
			public Guid LanguageId
			{
				get
				{
					//If _languageId = Guid.Empty Then
					dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_LANGUAGE);
					if (dtrow != null)
					{
						_languageId = new Guid(dtrow["OptionValue"].ToString());
					}
					//End If
					//HACK: Afin de setter le language ID Global quand on le sais
					//Quand nous ne somme pas authentifier avec le user dans l'application on ne le sait pas pour Metaquery.GetSSPMetaQuery
					AppManager.LanguageId = _languageId;
					//
					return _languageId;
				}
				set
				{
					_languageId = value;
				}
			}
			//
			public string LanguageName
			{
				get
				{
					return _languageName;
				}
				set
				{
					_languageName = value;
				}
			}
			//
			public int DescriptionColumnIndex
			{
				get
				{
					if (_descriptionColumnIndex == 0)
					{
						dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_DESCRIPTION);
						if (dtrow != null)
						{
							if (dtrow["OptionValue"].ToString() == "ALT")
							{
								_descriptionColumnIndex = 3;
							}
							else
							{
								_descriptionColumnIndex = 2;
							}
						}
						else
						{
							_descriptionColumnIndex = 2;
						}
					}
					return _descriptionColumnIndex;
				}
				set
				{
					_descriptionColumnIndex = value;
				}
			}
		}
	}
	
}
